package com.baoleme.common;



import com.baoleme.exceptions.enumeration.ResponseEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**

 */
@Data
@NoArgsConstructor
@AllArgsConstructor
/*统一返回前端的数据类型
 * 其中包含是否成功，响应代码，响应信息，响应的具体数据（响应失败时为空）*/
public class Result<T> {
    private Boolean requestStatus;//
    private Integer code;
    private String message;
    private T data;

    public static Result<Object> success() {
        return success(null);
    }

    public static <T> Result<T> success(T t) {
        return success(ResponseEnum.SUCCESS, t);
    }

    public static <T> Result<T> success(ResponseEnum response, T t) {
        Result<T> result = new Result<>();
        result.requestStatus = true;
        result.code = response.getCode();
        result.message = response.getMessage();
        result.data = t;
        return result;
    }

    public static <T> Result<T> error(ResponseEnum response) {
        return error(response, null);
    }

    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.requestStatus = false;
        result.code = -1;
        result.message = message;
        result.data = null;
        return result;
    }


    public static <T> Result<T> error(ResponseEnum response, T t) {
        Result<T> result = new Result<>();
        result.requestStatus = false;
        result.code = response.getCode();
        result.message = response.getMessage();
        result.data = t;
        return result;
    }
}
